<?php

namespace App\Http\Controllers\Home;

use Illuminate\Http\Request;
use DB;
use App\Http\Requests;
use App\Http\Controllers\Controller;

class DetailsController extends Controller
{
    public function show($id)
    {
        // dd($id);

        // 查询根分类的所有店铺信息放到导航栏
        $cateinfo=DB::table('cate')->where('pid',0)->get();

        // 查询友情链接
        $link=DB::table('link')->get();
    	


        $data = DB::table('goods as g')->where('g.id',$id)-> select('g.goodsname','g.logo','g.value','g.inventory','g.id','g.sellnum','g.isshop')->first();
        // dd($data);
        $shopid = $data->isshop;
        // dd($shopid);
        $shopname = DB::table('shop')->where('id',$shopid)->first()->shopname;
        // dd($shopname);
    	return view('home.details.details',['title'=>'商品详情页','data' => $data, 'cateinfo'=>$cateinfo, 'links'=>$link,'shopname'=>$shopname,'shopid'=>$shopid]);
    }

    public function insert(Request $request)
    {
    	$data = $request -> except('_token');
        // dd($data);
    	$res = DB::table('goodscar') -> insert($data);
    	$gid = $data['gid'];
    	if($data['uid']==0)
    	{
    			echo '<script>alert("请登录后添加购物车");window.location.href="/home/user/add";</script>';
    	}
    	// dd($gid);
    	if($res)
    	{
    		echo '<script>alert("加入购物车成功");window.location.href="/home/details/'.$gid.'";</script>';
    	}else{
    		echo '<script>alert("加入购物车失败");window.location.href="/home/details/'.$gid.'";</script>';
    	

    	}
    }

    public function car($id)
    {
        $data = DB::table('goodscar')-> where('uid',$id) -> where('status','0') -> get();
        // var_dump($data);
      
        return view('home.details.car',['data' => $data]);
    }
}
